import './assets/main.css';

import { createPinia } from 'pinia';
import type { QiankunProps } from 'vite-plugin-qiankun/dist/helper';
import { qiankunWindow, renderWithQiankun } from 'vite-plugin-qiankun/dist/helper';
import { createApp } from 'vue';
import { name } from '../package.json';
import App from './App.vue';
import router, { destoryHistory } from './router';
import VXETable from 'vxe-table';
import VXETablePluginExportXLSX from '@/views/Demo/Table/utils/VXETablePluginExportXLSX';
import 'vxe-table/lib/style.css';
import './styles/index.less';
import './assets/font/iconfont.css';
import './assets/font/iconfont.js';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';

import 'v-contextmenu/dist/themes/default.css';
import clickOutside from '@/views/Demo/Table/utils/clickOutside';
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

let instance: any = null;
VXETable.use(VXETablePluginExportXLSX);

function render(props: QiankunProps = {}) {
  const { container } = props;
  const app = createApp(App);
  app.use(createPinia());
  app.use(router);
  app.use(VXETable);
  app.use(ElementPlus, {
    locale: zhCn,
  });
  app.directive('click-outside', clickOutside);

  instance = app;
  const mountQuery = container ? container : `#${name}`;
  app.mount(mountQuery);
}
renderWithQiankun({
  mount(props: QiankunProps) {
    history.replaceState(null, '');
    render(props);
  },
  bootstrap() {
    console.log('bootstrap');
  },
  unmount(props: any) {
    console.log('unmount');
    history.replaceState(null, '');
    destoryHistory();
    instance && instance.unmount();
    instance = null;
  },
  update(props: QiankunProps) {},
});

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render();
}
